<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="closeDialog"
    @open="openDialog"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="roleIds" v-loading="loading">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>

    </el-checkbox-group>

    <template #footer>
      <el-button type="primary" size="small" @click="clickSubmit">确定</el-button>
      <el-button size="small" @click="closeDialog">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { reqGetRoleList } from '@/api/setting'
import { reqGetUserDetailById } from '@/api/user'
import { reqAssignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      roleIds: [], // 默认选中的已有角色
      list: [], // 所有角色列表的数据
      loading: false
    }
  },
  methods: {
    closeDialog() {
      this.$emit('update:showRoleDialog', false)
    },
    async GetRoleList() {
      const { data } = await reqGetRoleList(1, 200)
      console.log(data)
      this.list = data.rows
    },
    async getUserDetailById() {
      const { data } = await reqGetUserDetailById(this.userId)
      console.log(data)
      this.roleIds = data.roleIds
      console.log(this.roleIds)
    },
    //	Dialog 打开的回调 对话框打开时的方法
    async openDialog() {
      this.loading = true
      //   await this.GetRoleList()
      //   await this.getUserDetailById()
      Promise.all([this.GetRoleList(), this.getUserDetailById()]).then(() => {
        this.loading = false
      })
    },
    // 点击确定按钮保存
    async clickSubmit() {
      await reqAssignRoles({
        id: this.userId,
        roleIds: this.roleIds
      })
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>
<style lang="scss" scoped>
.assign-role {
  ::v-deep {
    .el-checkbox {
      font-size: 30px;
    }
  }
}
</style>
